package com.example.experiment4sqlite

import android.content.Intent
import android.os.Bundle
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.experiment4sqlite.adapter.MyAdapter
import com.example.experiment4sqlite.bean.UserInfo
import com.example.experiment4sqlite.utils.UserDbHelper

class FindAndDelUserActivity : AppCompatActivity() {
    private lateinit var recyclerView: RecyclerView
    private lateinit var viewAdapter: RecyclerView.Adapter<*>
    private lateinit var viewManager: RecyclerView.LayoutManager

    private lateinit var myDataset: List<UserInfo>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.activity_find_and_del_user)

        val user = UserDbHelper(this)
        val db = user.readableDatabase
        myDataset = user.queryData(db)!!

        viewManager = LinearLayoutManager(this)
        viewAdapter = MyAdapter(myDataset)

        // 找到该id，设置布局管理器，适配器
        recyclerView = findViewById<RecyclerView>(R.id.recycle_view).apply {
            // for performance?
            setHasFixedSize(true)
            layoutManager = viewManager
            adapter = viewAdapter
        }
    }

    fun refresh() {
        finish()
        val intent = Intent(this, FindAndDelUserActivity::class.java)
        startActivity(intent)
    }
}
